import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
    selector: 'sa-renewal-log',
    templateUrl: './renewal-log.component.html',
    styleUrls: ['./renewal-log.component.css']
})
export class RenewalLogComponent implements OnInit {
    tableData: any = []; // table的表单数据
    totalCounts: number = 0; //分页总条数
    curPage: number = 1; //分页当前页
    pageSize: number = 10; //内置的分页每页条数
    // 查询参数
    tableSearchParam: any = {
        companyName: '',
        deviceId: '',
        carNumber: '',
        startTime: '',
        endTime: '',
    }
    constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
        activatedRoute.queryParams.subscribe(queryParams => {});
    }
    ngOnInit() {
        // this.load();
    }

    //初始化车辆
    initCarTree() {}
    //公司[项目]选中回调
    companySelected(data) {
        this.tableSearchParam.deviceId = data.id;
        this.tableSearchParam.carNumber = data.text;
    }
    // 时间选择回调
    dateSelect(time) {
        this.tableSearchParam.startTime = time.startTime;
        this.tableSearchParam.endTime = time.endTime;
        this.getData()
    }
    // 同步
    getData(){
        if(!this.tableSearchParam.startTime||!this.tableSearchParam.endTime){
            this.jsonApiService.message('请选择时间范围');
            return
        }
        let param = {
            "pageSize": this.pageSize,
            "nowPage": this.curPage,
            "parameters": {
                carNumber: this.tableSearchParam.carNumber,
                deviceId: this.tableSearchParam.deviceId,
                companyName: this.tableSearchParam.companyName,
                startTime: this.tableSearchParam.startTime,
                endTime: this.tableSearchParam.endTime,
                type: 1,
            }
        }
        let reqdata = 'gridPager=' + JSON.stringify(param);
        this.jsonApiService.http("renewal/renewalLogList.html", reqdata).subscribe(result => {
            if (result.isSuccess && result.exhibitDatas) {
                this.tableData = result.exhibitDatas
                this.totalCounts = result.recordCount;
            }
        });
    }
    // 获取列表数据
    load() {
        let param = {
            "pageSize": this.pageSize,
            "nowPage": this.curPage,
            "parameters": {
                carNumber: this.tableSearchParam.carNumber,
                deviceId: this.tableSearchParam.deviceId,
                companyName: this.tableSearchParam.companyName,
                startTime: this.tableSearchParam.startTime,
                endTime: this.tableSearchParam.endTime,
                type: 0,
            }
        }
        let reqdata = 'gridPager=' + JSON.stringify(param);
        this.jsonApiService.http("renewal/renewalLogList.html", reqdata).subscribe(result => {
            if (result.isSuccess && result.exhibitDatas) {
                this.tableData = result.exhibitDatas
                this.totalCounts = result.recordCount;
            }
        });
    }

    // 分页回调
    paginate(e) {
//      this.activeSelected = '';
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
    // 刷新表
    refresh() {
        this.load();
    }
    // 表单查询
    tableSearch() {
        this.curPage = 1;
        this.load();
    }

    // 数据导出
	exportData(){
		let param = {
            "pageSize": this.pageSize,
            "nowPage": this.curPage,
            "parameters": {
                carNumber: this.tableSearchParam.carNumber,
                deviceId: this.tableSearchParam.deviceId,
                companyName: this.tableSearchParam.companyName,
                startTime: this.tableSearchParam.startTime,
                endTime: this.tableSearchParam.endTime,
                export:"1"
            }
        }
        let timeCheck = common.correctTime(param.parameters, ['export'])
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
        common.exprotDataFile(param, 'gridPager', config.BaseURL + 'renewal/renewalLogList.html')	
    }
}   